package com.csair.common.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

public final class IOUtils {

    public static String inputStreamToString(InputStream inputStream) {
        if (inputStream == null) {
            return "";
        }
        InputStreamReader streamReader = null;
        BufferedReader reader = null;
        StringBuilder result = new StringBuilder();
        try {
            streamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(streamReader);
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line).append("\n");
            }
        } catch (IOException e) {
            LogUtils.printStackTrace(e);
        } finally {
            close(reader);
            close(streamReader);
            close(inputStream);
        }
        return result.toString();
    }

    /**
     * 将InputStream转换成byte数组
     *
     * @param in InputStream
     * @return byte[]
     * @throws IOException
     */
    public static byte[] inputStreamToByte(InputStream in) throws IOException {
        final int BUFFER_SIZE = 1024;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] data = new byte[BUFFER_SIZE];
        int count;
        while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
            outStream.write(data, 0, count);
        outStream.close();
        return outStream.toByteArray();
    }

    public static boolean stringToFile(File targetFile, String resource,
                                       boolean append) {
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(targetFile, append);
            outputStream.write(resource.getBytes());
            outputStream.flush();
            return true;
        } catch (IOException e) {
            LogUtils.printStackTrace(e);
        } finally {
            close(outputStream);
        }
        return false;
    }

    public static boolean stringToFile(String targetFileName, String resource,
                                       boolean append) {
        File targetFile = new File(targetFileName);
        try {
            if (!targetFile.exists() && !targetFile.createNewFile()) {
                return false;
            }
        } catch (IOException e) {
            LogUtils.printStackTrace(e);
            return false;
        }
        return stringToFile(targetFile, resource, append);
    }

    public static boolean copyFile(File sourceFile, File targetFile,
                                   boolean append) {
        if (sourceFile == null || !sourceFile.exists() || targetFile == null
                || !targetFile.exists()) {
            return false;
        }
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(sourceFile);
            outputStream = new FileOutputStream(targetFile, append);
            byte[] buf = new byte[1024];
            int len;
            while ((len = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.flush();
            return true;
        } catch (FileNotFoundException e) {
            LogUtils.printStackTrace(e);
        } catch (IOException e) {
            LogUtils.printStackTrace(e);
        } finally {
            close(inputStream);
            close(outputStream);
        }
        return false;
    }

    public static boolean copyFile(String sourceFileName, String targetFileName) {
        File sourceFile = new File(sourceFileName);
        if (!sourceFile.exists() || !sourceFile.isFile()) {
            return false;
        }
        File targetFile = new File(targetFileName);
        try {
            if (!targetFile.exists() && !targetFile.createNewFile()) {
                return false;
            }
        } catch (IOException e) {
            LogUtils.printStackTrace(e);
            return false;
        }
        return copyFile(sourceFile, targetFile, true);
    }

    public static boolean close(InputStream inputStream) {
        if (inputStream != null) {
            try {
                inputStream.close();
                return true;
            } catch (IOException e) {
                LogUtils.printStackTrace(e);
                return false;
            }
        }
        return true;
    }

    public static boolean close(OutputStream outputStream) {
        if (outputStream != null) {
            try {
                outputStream.close();
                return true;
            } catch (IOException e) {
                LogUtils.printStackTrace(e);
                return false;
            }
        }
        return true;
    }

    public static boolean close(Reader reader) {
        if (reader != null) {
            try {
                reader.close();
                return true;
            } catch (IOException e) {
                LogUtils.printStackTrace(e);
                return false;
            }
        }
        return true;
    }

    public static boolean close(Writer writer) {
        if (writer != null) {
            try {
                writer.close();
                return true;
            } catch (IOException e) {
                LogUtils.printStackTrace(e);
                return false;
            }
        }
        return true;
    }
}
